USE [Musicman]
GO
/****** Object:  StoredProcedure [dbo].[ClearPreviousResults]    Script Date: 11/15/2008 22:10:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[AddPendingDownload]
	@UserID bigint,
	@SearchFileID bigint
AS
BEGIN
 IF NOT EXISTS(SELECT * FROM MUSICMAN.DBO.USERDOWNLOADS WHERE USERID = @USERID 
						AND SEARCHFIELID = @SearchFileID)

 BEGIN
	INSERT INTO MUSICMAN.DBO.USERDOWNLOADS (USERID, SEARCHFIELID) VALUES (@UserID, @SearchFileID)
 END

END



set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[AddPendingDownload]
	@UserID bigint,
	@SearchFileID bigint
AS
BEGIN

BEGIN TRANSACTION;
BEGIN TRY	

 IF NOT EXISTS(SELECT * FROM MUSICMAN.DBO.USERDOWNLOADS WHERE USERID = @USERID 
						AND SEARCHFIELID = @SearchFileID)
 BEGIN
	INSERT INTO MUSICMAN.DBO.USERDOWNLOADS (USERID, SEARCHFIELID) VALUES (@UserID, @SearchFileID)
 END

 IF NOT EXISTS(SELECT * FROM MUSICMAN.DBO.FILESSTATS WHERE SEARCHFILEID = @SearchFileID)
 BEGIN
	INSERT INTO MUSICMAN.DBO.FILESSTATS(SEARCHFILEID, TOTALDOWNLOADS) VALUES (@SearchFileID, 1);
 END 
 ELSE 
 BEGIN 
    UPDATE MUSICMAN.DBO.FILESSTATS SET TOTALDOWNLOADS = TOTALDOWNLOADS + 1 WHERE SEARCHFILEID = @SearchFileID
 END

COMMIT TRANSACTION;
END TRY
BEGIN CATCH
	ROLLBACK TRANSACTION;

    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT @ErrorMessage = ERROR_MESSAGE();
    SELECT @ErrorSeverity = ERROR_SEVERITY();
    SELECT @ErrorState = ERROR_STATE();
	RAISERROR (@ErrorMessage, -- Message text.
                   @ErrorSeverity, -- Severity.
                   @ErrorState -- State.
                   );
END CATCH

END



  